﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Grizzy_X
{
    static class Transformer
    {
        public static double Displayscale = 1;
        public static double OffsetX = 0;      //Offset为屏幕左上角的实际坐标
        public static double OffsetY = Controller.MyMap.Height;

        public static Point ToScreen(PointF real)
        {
            Point screen = new Point();
            if (Controller.MyMap.Unit == "英寸")
            {
                screen.X = (int)Math.Round((real.X - OffsetX) * 96 * Displayscale);
                screen.Y = (int)Math.Round((OffsetY - real.Y) * 96 * Displayscale);
            }
            else
            {
                screen.X = (int)Math.Round((real.X - OffsetX) * 96 * Displayscale / 2.54);
                screen.Y = (int)Math.Round((OffsetY - real.Y) * 96 * Displayscale / 2.54);
            }
            return screen;
        }

        public static PointF FromScreen(Point screen)
        {
            PointF real = new PointF();
            if (Controller.MyMap.Unit == "英寸")
            {
                real.X = (float)(screen.X / (Displayscale * 96) + OffsetX);
                real.Y = (float)(OffsetY - screen.Y / (Displayscale * 96));
            }
            else
            {
                real.X = (float)(screen.X * 2.54 / (Displayscale * 96) + OffsetX);
                real.Y = (float)(OffsetY - screen.Y * 2.54 / (Displayscale * 96));
            }
            return real;
        }

        public static bool Reset(Viewer View)
        {
            try
            {
                Displayscale = View.GetScale();
                OffsetX = View.GetX();
                OffsetY = View.GetY();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
